#include "chttpparser.h"

int on_message_begin(http_parser* http)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_message_begin();
    return 0;
}
int on_status(http_parser* http, const char* at, size_t length)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_status(at, length);
    return 0;
}
int on_url(http_parser* http, const char* at, size_t length)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_url(at, length);
    return 0;
}
int on_header_field(http_parser* http, const char* at, size_t length)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_header_field(at, length);
    return 0;
}
int on_header_value(http_parser* http, const char* at, size_t length)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_header_value(at, length);
    return 0;
}
int on_headers_complete(http_parser* http)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_header_complete();
    return 0;
}

int on_body(http_parser* http, const char* at, size_t length)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_body(at, length);
    return 0;
}

int on_message_complete(http_parser* http)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_message_complete();
    return 0;
}
int on_chunk_header(http_parser* http)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_chunk_header();
    return 0;
}
int on_chunk_complete(http_parser* http)
{
    CHttpParser* p = static_cast<CHttpParser*>(http->data);
    if (!p) {
        return 0;
    }
    p->on_http_chunk_complete();
    return 0;
}

CHttpParser::CHttpParser()
{
    m_parser = NULL;
    m_parser = (http_parser*)malloc(sizeof(http_parser));
    if (!m_parser)
        return;

    m_httpcb.on_message_begin = on_message_begin;
    m_httpcb.on_status = on_status;
    m_httpcb.on_url = on_url;
    m_httpcb.on_header_field = on_header_field;
    m_httpcb.on_header_value = on_header_value;
    m_httpcb.on_headers_complete = on_headers_complete;
    m_httpcb.on_body = on_body;
    m_httpcb.on_message_complete = on_message_complete;
    m_httpcb.on_chunk_header = on_chunk_header;
    m_httpcb.on_chunk_complete = on_chunk_complete;
}
CHttpParser::~CHttpParser(void)
{
    if (m_parser) {
        free(m_parser);
    }
}
int CHttpParser::http_init_analyze(int type)
{
    if (type == http_request) {
        http_parser_init(m_parser, HTTP_REQUEST);
    } else if (type == http_response) {
        http_parser_init(m_parser, HTTP_RESPONSE);
    }
    return 1;
}
int CHttpParser::http_analyze(char* p, size_t ilen)
{
    m_parser->data = this;
    return http_parser_execute(m_parser, &m_httpcb, p, ilen);
}

void CHttpParser::on_http_message_begin()
{
}
void CHttpParser::on_http_status(const char* at, size_t length)
{
}
void CHttpParser::on_http_url(const char* at, size_t length)
{
}
void CHttpParser::on_http_header_field(const char* at, size_t length)
{
}
void CHttpParser::on_http_header_value(const char* at, size_t length)
{
}
void CHttpParser::on_http_header_complete()
{
}
void CHttpParser::on_http_body(const char* at, size_t length)
{
}
void CHttpParser::on_http_message_complete()
{
}
void CHttpParser::on_http_chunk_header()
{
}
void CHttpParser::on_http_chunk_complete()
{
}

int CHttpParser::isConnectKeeplive()
{
    return http_should_keep_alive(m_parser);
}
